package us.icitap.cm.business.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.eclipse.persistence.annotations.Customizer;

import us.icitap.cm.business.entities.regmin.Cities;
import us.icitap.cm.business.entities.regmin.District;
import us.icitap.cm.business.entities.regmin.Region;
import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.utils.MyCustomizer;

@Entity
@Customizer(MyCustomizer.class)
@Table(name="PLACE", schema="MPC")
public class Place implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2785510201670607557L;


	@Id
	@Column(insertable=false, updatable = false)
	private Integer id;
	

	@OneToOne
	@JoinColumn(name="ID",nullable=false)
	private AppEntity appEntity;
	
	@ManyToOne
	@JoinColumn(name="PLACE_CLASSIFICATION_ID")
	private AppList placeClassification;
	
	@ManyToOne
	@JoinColumn(name="PLACE_TYPE_ID")
	private AppList placeType;
	
	@ManyToOne
	@JoinColumn(name="STREET_ID")
	private Street street;
	
	@Column(name="NUM")
	private String number;
	
	private String apartment;
	
	@ManyToOne
	@JoinColumn(name="STATE_ID")
	private States states;
	
	@ManyToOne
	@JoinColumns({@JoinColumn(name = "CODE_STATE", referencedColumnName = "CODE_STATE"), @JoinColumn(name = "CODE_CITY", referencedColumnName = "CODE_CITY")})
	private Cities cities;
	
	@ManyToOne
	@JoinColumn(name="DISTRICT_ID")
	private District district;
	
	@ManyToOne
	@JoinColumn(name="REGION_ID")
	private Region region;
	

	@Column(name="ADDRESS_TEXT", length=400)
	private String addressText;

	@Column(name="LATITUDE")
	private double latitude;
	
	@Column(name="LONGITUDE")
	private double longitude;

	public Place() {
		
	}
	
	public Place(Place place) {
		this.addressText = place.getAddressText();
		this.apartment = place.getApartment();
		this.cities = place.getCities();
		this.district = place.getDistrict();
		this.number = place.getNumber();
		this.placeClassification = place.getPlaceClassification();
		this.placeType = place.getPlaceType();
		this.region = place.getRegion();
		this.states = place.getStates();
		this.street = place.getStreet();
		this.latitude = place.getLatitude();
		this.longitude = place.getLongitude();
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public AppEntity getAppEntity() {
		return appEntity;
	}


	public void setAppEntity(AppEntity appEntity) {
		this.appEntity = appEntity;
	}


	public String getAddressText() {
		return addressText;
	}


	public void setAddressText(String addressText) {
		this.addressText = addressText;
	}


	public Street getStreet() {
		return street;
	}


	public void setStreet(Street street) {
		this.street = street;
	}


	public String getNumber() {
		return number;
	}


	public void setNumber(String number) {
		this.number = number;
	}


	public String getApartment() {
		return apartment;
	}


	public void setApartment(String apartment) {
		this.apartment = apartment;
	}


	public States getStates() {
		return states;
	}


	public void setStates(States states) {
		this.states = states;
	}


	public Cities getCities() {
		return cities;
	}


	public void setCities(Cities cities) {
		this.cities = cities;
	}


	public District getDistrict() {
		return district;
	}


	public void setDistrict(District district) {
		this.district = district;
	}


	public Region getRegion() {
		return region;
	}


	public void setRegion(Region region) {
		this.region = region;
	}


	@Override
	public String toString() {
		String s;
		if(this.street != null) {
			return street.getTag();
		}
		if(this.states != null) {
			s= states.getTag();
			if(cities != null) {
				s+= ", " + cities.getTag();
				return s;
			}
		}
		if(addressText != null) return addressText;
		return "Vendi nr." + id;
	}


	public AppList getPlaceClassification() {
		return placeClassification;
	}


	public void setPlaceClassification(AppList placeClassification) {
		this.placeClassification = placeClassification;
	}


	public AppList getPlaceType() {
		return placeType;
	}


	public void setPlaceType(AppList placeType) {
		this.placeType = placeType;
	}


	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((addressText == null) ? 0 : addressText.hashCode());
		result = prime * result
				+ ((apartment == null) ? 0 : apartment.hashCode());
		result = prime * result + ((cities == null) ? 0 : cities.hashCode());
		result = prime * result
				+ ((district == null) ? 0 : district.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		result = prime
				* result
				+ ((placeClassification == null) ? 0 : placeClassification
						.hashCode());
		result = prime * result
				+ ((placeType == null) ? 0 : placeType.hashCode());
		result = prime * result + ((region == null) ? 0 : region.hashCode());
		result = prime * result + ((states == null) ? 0 : states.hashCode());
		result = prime * result + ((street == null) ? 0 : street.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Place other = (Place) obj;
		if (addressText == null) {
			if (other.addressText != null)
				return false;
		} else if (!addressText.equals(other.addressText))
			return false;
		if (apartment == null) {
			if (other.apartment != null)
				return false;
		} else if (!apartment.equals(other.apartment))
			return false;
		if (cities == null) {
			if (other.cities != null)
				return false;
		} else if (!cities.equals(other.cities))
			return false;
		if (district == null) {
			if (other.district != null)
				return false;
		} else if (!district.equals(other.district))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		if (placeClassification == null) {
			if (other.placeClassification != null)
				return false;
		} else if (!placeClassification.equals(other.placeClassification))
			return false;
		if (placeType == null) {
			if (other.placeType != null)
				return false;
		} else if (!placeType.equals(other.placeType))
			return false;
		if (region == null) {
			if (other.region != null)
				return false;
		} else if (!region.equals(other.region))
			return false;
		if (states == null) {
			if (other.states != null)
				return false;
		} else if (!states.equals(other.states))
			return false;
		if (street == null) {
			if (other.street != null)
				return false;
		} else if (!street.equals(other.street))
			return false;
		return true;
	}


	public boolean myEquals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Place other = (Place) obj;
		if (addressText == null) {
			if (other.addressText != null)
				return false;
		} else if (!addressText.equals(other.addressText))
			return false;
		if (apartment == null) {
			if (other.apartment != null)
				return false;
		} else if (!apartment.equals(other.apartment))
			return false;
		if (cities == null) {
			if (other.cities != null)
				return false;
		} else if (!cities.equals(other.cities))
			return false;
		if (district == null) {
			if (other.district != null)
				return false;
		} else if (!district.equals(other.district))
			return false;
		
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		if (placeClassification == null) {
			if (other.placeClassification != null)
				return false;
		} else if (!placeClassification.equals(other.placeClassification))
			return false;
		if (placeType == null) {
			if (other.placeType != null)
				return false;
		} else if (!placeType.equals(other.placeType))
			return false;
		if (region == null) {
			if (other.region != null)
				return false;
		} else if (!region.equals(other.region))
			return false;
		if (states == null) {
			if (other.states != null)
				return false;
		} else if (!states.equals(other.states))
			return false;
		if (street == null) {
			if (other.street != null)
				return false;
		} else if (!street.equals(other.street))
			return false;
		return true;
	}

	private double getLatitude() {
		return latitude;
	}

	private void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	private double getLongitude() {
		return longitude;
	}

	private void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	
}
